/* 全局对象用于缓存赛事数据 */
var match = {
  data: null   //赛事数据
};

App.controller('AppController', ['$rootScope', '$scope', 'getService', 'cfpLoadingBar', '$timeout', '$state', function($rootScope, $scope, getService, cfpLoadingBar, $timeout, $state) {

  $scope.$root.title = '赛事列表'; //动态设置页面标题 

  //获取到赛事数据后，立即缓存至全局对象内，便于后面使用
  getService.dataList()
  .success(function(data, status) {
    match.data = $scope.matchInfo = data['matchInfo'];
  })
  .error(function(data, status) {
    console.log("error");
  });

	$scope.start = function() {
    cfpLoadingBar.start();
  };

  $scope.complete = function () {
    cfpLoadingBar.complete();
  };

  // fake the initial load so first time users can see the bar right away:
  $scope.start();
  // $scope.fakeIntro = true;
  // $timeout(function() {
    $scope.complete();
    // $scope.fakeIntro = false;
  // }, 1250);
  
}]);


App.controller('MatchSubjectController', ['$scope', '$state', function($scope, $state) {
	
  $scope.$root.title = '科目选择'; //动态设置页面标题 

  $scope.matchId = $state.params.matchId; //获取当前赛事ID
  $scope.subjects = { }; //用于缓存当前赛事所有信息
  
  /* 遍历赛事数组取出当前赛事所有信息 */
  angular.forEach(match['data'], function(value, index){
    if (value.matchId == $scope.matchId) {
      $scope.subjects = value;
    }
  });

  $scope.mini = function(matchId, index) {
    if (index==0) {
      $state.go('minipersonal', {matchId: matchId, index: index});
    }
    if (index==1) {
      $state.go('miniteam', {matchId: matchId, index: index});
    }
  };

}]);


App.controller('MatchScoreController', ['$scope', '$state', '$http', 'postService', function($scope, $state, $http, postService) {
 
  $scope.$root.title = '成绩列表'; //动态设置页面标题 

  $scope.matchId    = $state.params.matchId; //获取当前赛事ID
  $scope.subjectIds = $state.params.subjectId; //获取当前赛事科目ID
  $scope.subjects   = { }; //缓存当前赛事所有信息
  $scope.scores     = { }; //缓存当前赛事当前科目中个人成绩列表

  /* 遍历赛事数组取出当前赛事所有信息 */
  angular.forEach(match['data'], function(value, index){
    if (value.matchId == $scope.matchId) {
      $scope.subjects = value;
    }
  });
  
  // postService.dataList({matchId: $scope.matchId, subjectId: $scope.subjectIds})
  $http.get('./api/subject.json')
  .success(function(data, status) {
    $scope.scores = data.scores;
    console.log($scope.scores)
  })
  .error(function(data, status) {
    console.log('error')
  });

}]);


App.controller('ScorePersonController', ['$scope', '$state', '$http', 'postService', function($scope, $state, $http, postService) {
 
  $scope.$root.title = '个人成绩'; //动态设置页面标题 

  $scope.alert    = true;
  $scope.matchId    = $state.params.matchId; //获取当前赛事ID
  $scope.subjectIds = $state.params.subjectId; //获取当前赛事科目ID
  $scope.userId     = $state.params.userId; //获取当前选手ID
  $scope.subjects   = { }; //获取当前赛事所有信息
  $scope.scores     = { }; //获取当前赛事当前科目中个人成绩列表

  /* 遍历赛事数组取出当前赛事所有信息 */
  angular.forEach(match['data'], function(value, index){
    if (value.matchId == $scope.matchId) {
      $scope.subjects = value;
    }
  });

  // postService.dataList({matchId: $scope.matchId, subjectId: $scope.subjectIds})
  $http.get('./api/subject.json')
  .success(function(data, status) {
    var itemData = null; //用于缓存个人成绩
    $scope.scores = data.scores;

    //遍历符合条件的个人成绩
    angular.forEach($scope.scores, function(value, index){
      if(value.userId == $scope.userId) {
        itemData = value;
      }
    });
    
    $scope.userName    = itemData['userName'];    //获取个人姓名
    $scope.userSex     = itemData['userSex'];     //获取个人性别
    $scope.score       = itemData['score'];       //获取个人成绩
    $scope.ranking     = itemData['ranking'];     //获取个人排名
    $scope.NO          = itemData['NO'];          //获取个人参赛号
    $scope.realScore   = itemData['realScore'];   //获取个人净成绩
    $scope.personalAch = itemData['personalAch']; //获取个人成绩详情
  })
  .error(function(data, status) {
    console.log('error')
  });
  
}]);


App.controller('ScoreInquiryController', ['$scope', '$state', function($scope, $state) {

  $scope.$root.title = '成绩查询'; //动态设置页面标题 

  $scope.matchId    = $state.params.matchId; //获取当前赛事ID
  $scope.subjectIds = $state.params.subjectId; //获取当前赛事科目ID
  $scope.subjects   = { }; //获取当前赛事所有信息

  /* 遍历赛事数组取出当前赛事所有信息 */
  angular.forEach(match['data'], function(value, index){
    if (value.matchId == $scope.matchId) {
      $scope.subjects = value;
    }
  });

  //表单数据选手号、选手姓名、团队名称
  $scope.user = {
    userId: '',
    userName: '',
    teamName: ''
  };

  //表单提交
  $scope.submitForm = function() {
    /* 如果获取到选手号，则查询个人成绩 */
    if ($scope.user.userId !== '') {
      $state.go('personal', {matchId: $scope.matchId, subjectId: $scope.subjectIds, flag: $scope.user.userId});
    }

    /* 如果获取到团队名称，则查询团队成绩 */
    else if ($scope.user.teamId !== '') {
      $state.go('team', {matchId: $scope.matchId, subjectId: $scope.subjectIds});
      store.set('teamName',$scope.user.teamName); //本地缓存团队名称
    }
  };

}]);


App.controller('InquiryListController', ['$scope', '$state', '$http', 'postService', function($scope, $state, $http, postService) {

  $scope.$root.title = '个人成绩'; //动态设置页面标题 

  $scope.matchId    = $state.params.matchId; //获取当前赛事ID
  $scope.subjectIds = $state.params.subjectId; //获取当前赛事科目ID
  $scope.flag       = $state.params.flag; //获取当前选手ID
  $scope.subjects   = { }; //获取当前赛事所有信息
  $scope.scores     = { }; //获取当前赛事当前科目中个人成绩列表

  /* 遍历赛事数组取出当前赛事所有信息 */
  angular.forEach(match['data'], function(value, index){
    if (value.matchId == $scope.matchId) {
      $scope.subjects = value;
    }
  });

  // postService.dataList({matchId: $scope.matchId, subjectId: $scope.subjectIds})
  $http.get('./api/subject.json')
  .success(function(data, status) {
    var itemData = null; //用于缓存个人成绩
    $scope.scores = data.scores;

    //遍历符合条件的个人成绩
    angular.forEach($scope.scores, function(value, index){
      if(value.userId == $scope.flag) {
        itemData = value;
      }
    });

    if (!itemData) {
      $scope.alert = false;
    } else {
      $scope.alert = true;

      $scope.userName    = itemData['userName'];    //获取个人姓名
      $scope.userSex     = itemData['userSex'];     //获取个人性别
      $scope.score       = itemData['score'];       //获取个人成绩
      $scope.ranking     = itemData['ranking'];     //获取个人排名
      $scope.NO          = itemData['NO'];          //获取个人参赛号
      $scope.realScore   = itemData['realScore'];   //获取个人净成绩
      $scope.personalAch = itemData['personalAch']; //获取个人成绩详情
    }

  })
  .error(function(data, status) {
    console.log('error')
  });
  
}]);


App.controller('InquiryTeamController', ['$scope', '$state', '$http', 'postService', function($scope, $state, $http, postService) {

  $scope.$root.title = '团队列表'; //动态设置页面标题 

  $scope.flag       = store.get('teamName'); //获取本地缓存团队名称，用于过滤词
  $scope.matchId    = $state.params.matchId; //获取当前赛事ID
  $scope.subjectIds = $state.params.subjectId; //获取当前赛事科目ID
  $scope.subjects   = { }; //获取当前赛事所有信息
  $scope.scores     = { }; //获取当前赛事当前科目中个人成绩列表

  /* 遍历赛事数组取出当前赛事所有信息 */
  angular.forEach(match['data'], function(value, index){
    if (value.matchId == $scope.matchId) {
      $scope.subjects = value;
    }
  });

  // postService.dataList({matchId: $scope.matchId, subjectId: $scope.subjectIds})
  $http.get('./api/subject.json')
  .success(function(data, status) {
    var teamData = $scope.teamData = []; //用于缓存团队赛数据
    var name; //缓存团队名称比较是否同名
    $scope.scores = data.scores;

    /* 遍历当前赛事当前科目获取所有团队赛数据 */
    angular.forEach($scope.scores, function(value, index){
      if(value.teamName && value.teamName !== '' && value.teamName!==name) {
        name = value.teamName;
        teamData.push(value)
      }
    });

    $scope.teamlist = function(teamId) {
      $state.go('list', {matchId: $scope.matchId, subjectId: $scope.subjectIds, teamId: teamId});
    };

    console.log(teamData)

  })
  .error(function(data, status) {
    console.log('error')
  });

}]);


App.controller('InquiryTeamListController', ['$scope', '$state', '$http', 'postService', function($scope, $state, $http, postService) {

  $scope.$root.title = '团队成绩'; //动态设置页面标题 

  $scope.matchId    = $state.params.matchId; //获取当前赛事ID
  $scope.subjectIds = $state.params.subjectId; //获取当前赛事科目ID
  $scope.teamId     = $state.params.teamId; //获取当前团队ID
  $scope.subjects   = { }; //获取当前赛事所有信息
  $scope.scores     = { }; //获取当前赛事当前科目中个人成绩列表

  /* 遍历赛事数组取出当前赛事所有信息 */
  angular.forEach(match['data'], function(value, index){
    if (value.matchId == $scope.matchId) {
      $scope.subjects = value;
    }
  });

  // postService.dataList({matchId: $scope.matchId, subjectId: $scope.subjectIds})
  $http.get('./api/subject.json')
  .success(function(data, status) {
    $scope.teamData = []; //用于缓存团队赛数据
    $scope.scores = data.scores;

    /* 遍历当前赛事当前科目获取所有团队赛数据 */
    angular.forEach($scope.scores, function(value, index){
      if(value.teamId == $scope.teamId) {
        $scope.teamData.push(value)
      }
    });

  })
  .error(function(data, status) {
    console.log('error')
  });
   
}]);


App.controller('InquiryTeamPersonController', ['$scope', '$state', '$http', 'postService', function($scope, $state, $http, postService) {
  
  $scope.$root.title = '个人成绩'; //动态设置页面标题 

  $scope.alert      = true;
  $scope.matchId    = $state.params.matchId; //获取当前赛事ID
  $scope.subjectIds = $state.params.subjectId; //获取当前赛事科目ID
  $scope.userId     = $state.params.userId; //获取当前团队ID
  $scope.subjects   = { }; //获取当前赛事所有信息
  $scope.scores     = { }; //获取当前赛事当前科目中个人成绩列表

  /* 遍历赛事数组取出当前赛事所有信息 */
  angular.forEach(match['data'], function(value, index){
    if (value.matchId == $scope.matchId) {
      $scope.subjects = value;
    }
  });

  // postService.dataList({matchId: $scope.matchId, subjectId: $scope.subjectIds})
  $http.get('./api/subject.json')
  .success(function(data, status) {
    var teamData = $scope.teamData = { }; //用于缓存团队赛数据
    $scope.scores = data.scores;

    /* 遍历当前赛事当前科目获取所有团队赛数据 */
    angular.forEach($scope.scores, function(value, index){
      if(value.userId == $scope.userId) {
        teamData = $scope.teamData = value;
      }
    });

    $scope.userName    = teamData['userName'];    //获取个人姓名
    $scope.userSex     = teamData['userSex'];     //获取个人性别
    $scope.score       = teamData['score'];       //获取个人成绩
    $scope.ranking     = teamData['ranking'];     //获取个人排名
    $scope.NO          = teamData['NO'];          //获取个人参赛号
    $scope.realScore   = teamData['realScore'];   //获取个人净成绩
    $scope.personalAch = teamData['personalAch']; //获取个人成绩详情

    console.log($scope.teamData)
  })
  .error(function(data, status) {
    console.log('error')
  });

}]);


App.controller('MatchMiniPersonalController', ['$scope', '$state', '$http', function($scope, $state, $http) {
  
  $scope.$root.title = '成绩列表'; //动态设置页面标题 

  console.log($state.params)

  $scope.matchId    = $state.params.matchId; //获取当前赛事ID
  $scope.index      = $state.params.index; //获取当前赛事个人赛标识
  // $scope.subjectIds = $state.params.subjectId; //获取当前赛事科目ID
  $scope.subjects   = { }; //缓存当前赛事所有信息
  $scope.scores     = { }; //缓存当前赛事当前科目中个人成绩列表

  /* 遍历赛事数组取出当前赛事所有信息 */
  angular.forEach(match['data'], function(value, index){
    if (value.matchId == $scope.matchId) {
      $scope.subjects = value;
    }
  });
  
  console.log($scope.subjects)

  // postService.dataList({matchId: $scope.matchId, index: $scope.index})
  $http.get('./api/subject.json')
  .success(function(data, status) {
    $scope.scores = data.scores;
    console.log($scope.scores)
  })
  .error(function(data, status) {
    console.log('error')
  });

}]);


App.controller('MatchMiniTeamController', ['$scope', '$state', '$http', function($scope, $state, $http) {
  
  $scope.$root.title = '团队列表'; //动态设置页面标题 

  $scope.matchId    = $state.params.matchId; //获取当前赛事ID
  $scope.index      = $state.params.index; //获取当前赛事团队赛标识
  // $scope.subjectIds = $state.params.subjectId; //获取当前赛事科目ID
  $scope.subjects   = { }; //缓存当前赛事所有信息
  $scope.scores     = { }; //缓存当前赛事当前科目中个人成绩列表

  /* 遍历赛事数组取出当前赛事所有信息 */
  angular.forEach(match['data'], function(value, index){
    if (value.matchId == $scope.matchId) {
      $scope.subjects = value;
    }
  });

  // postService.dataList({matchId: $scope.matchId, index: $scope.index})
  $http.get('./api/subject.json')
  .success(function(data, status) {
    var teamData = $scope.teamData = []; //用于缓存团队赛数据
    var name; //缓存团队名称比较是否同名
    $scope.scores = data.scores;

    /* 遍历当前赛事当前科目获取所有团队赛数据 */
    angular.forEach($scope.scores, function(value, index){
      if(value.teamName && value.teamName !== '' && value.teamName!==name) {
        name = value.teamName;
        teamData.push(value)
      }
    });

    $scope.teamlist = function(teamId) {
      $state.go('list', {matchId: $scope.matchId, subjectId: '', teamId: teamId});
    };

    console.log(teamData)

  })
  .error(function(data, status) {
    console.log('error')
  });

  console.log($state.params)

}]);